var connect = require("connect");
var serveStatic = require("serve-static");
var fs = require("fs");
var http = require('http'); 

var app = connect();

app.use(serveStatic(__dirname + "/build"));

app.use(function(req, res, next) {
	// api 的场合 请求本地 "http://127.0.0.1:3001"
	if (req.url.indexOf('/api/') !== -1) {
		http.get("http://127.0.0.1:3001" + req.url,function(data){
		    data.on("data",function(res1){
		        res.end(res1);
		    })
		    data.on("end",function(){
		        res.end('http end');
		    })
		})
	} else {	
		// 路由的场合重新加载index.html
		fs.readFile(__dirname + '/build/index.html', function(err, data){
		    if(err){
		        console.log(err);
		        res.end('文件错误');
		    } else {
		        res.writeHead(200, {
		            'Content-type': 'text/html',
		            'Connection':'keep-alive'
		        });
		        res.end(data);
		    }
		})
	}
});

app.listen(8084);